//
//  SWTodayPoetriesView.swift
//  ChildStory
//
//  Created by linhan on 2016/12/9.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation


fileprivate let ListViewTitleHeight:CGFloat = 50
fileprivate let ListViewCellHeight:CGFloat = 60
fileprivate let LeftMargin:CGFloat = 15
fileprivate let RightMargin:CGFloat = 15

class SWTodayPoetriesView:UIView
{
    weak var delegate:ClickActionDelegate?
    
    var poetries:[SWPoetry] = []{
        didSet{
            updateView()
        }
    }
    
    private var _listView:UIView = UIView()
    private var _mainCell:SWPoetryViewMainCell?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        backgroundColor = UIColor.white
        
        let titleView:UIView = UIView()
        titleView.frame = CGRectMake(0, 0, width, ListViewTitleHeight)
        addSubview(titleView)
        
        let titleLabel = UILabel()
        titleLabel.text = "今日读物"
        titleLabel.textColor = UIColor(hex: 0x303332)
        titleLabel.font = UIFont.systemFont(ofSize: 18)
        titleLabel.sizeToFit()
        titleLabel.frame = CGRectMake(LeftMargin, (titleView.height - titleLabel.height) * 0.5, titleLabel.width, titleLabel.height)
        titleView.addSubview(titleLabel)
        
        //更多
        let moreTapGesture = UITapGestureRecognizer(target: self, action: #selector(moreBtnTapped))
        let moreView = UIView()
        moreView.height = titleView.height
        moreView.addGestureRecognizer(moreTapGesture)
        titleView.addSubview(moreView)
        
        let moreLabel:UILabel = UILabel()
        moreLabel.text = "更多"
        moreLabel.textColor = UIColor(hex: 0xababab)
        moreLabel.font = UIFont.systemFont(ofSize: 14)
        moreLabel.sizeToFit()
        moreLabel.center = CGPointMake(moreLabel.width * 0.5, moreView.height * 0.5)
        moreView.addSubview(moreLabel)
        
        let arrow:UIImageView = UIImageView(image: UIImage(named:"index_img_arrow_bgw"))
        arrow.center = CGPointMake(moreLabel.right + 9 + arrow.width * 0.5, moreView.height * 0.5)
        moreView.addSubview(arrow)
        moreView.width = arrow.right
        moreView.x = width - RightMargin - moreView.width
        
        
        let line:UIView = UIView()
        line.backgroundColor = UIColor(hex: 0xdfe2e1)
        line.frame = CGRectMake(0, titleView.height - 1, width, 1)
        titleView.addSubview(line)
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(cellTapped(_:)))
        _mainCell = SWPoetryViewMainCell(frame:CGRectMake(0, titleView.bottom, width, 0))
        _mainCell?.tag = 0
        _mainCell?.addGestureRecognizer(tapGesture)
        addSubview(_mainCell!)
        
        _listView.frame = CGRectMake(0, titleView.bottom, width, 0)
        addSubview(_listView)
    }
    
    private func updateView()
    {
        var poetries = self.poetries
        if let poetry = poetries.removeAtIndexSafely(0)
        {
            _mainCell?.poetry = poetry
        }
        
        _listView.y = _mainCell?.bottom ?? 0
        _listView.removeAllSubview()
        
        var lastY:CGFloat = 0
        let CellHeight:CGFloat = ListViewCellHeight
        for (i,poetry) in poetries.enumerated()
        {
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(cellTapped(_:)))
            let cellY:CGFloat = CellHeight * CGFloat(i)
            let cell = SWPoetryViewCell(frame: CGRectMake(0, cellY, width, CellHeight))
            cell.tag = (i + 1)
            cell.poetry = poetry
            cell.addGestureRecognizer(tapGesture)
            _listView.addSubview(cell)
            lastY = cell.bottom
            
            let line:UIView = UIView()
            line.backgroundColor = UIColor(hex: 0xdfe2e1)
            line.frame = CGRectMake(LeftMargin, cellY, width - LeftMargin - RightMargin, 1)
            _listView.addSubview(line)
        }
        _listView.height = lastY
        height = _listView.bottom
    }
    
    @objc private func cellTapped(_ gesture:UITapGestureRecognizer)
    {
        if let index = gesture.view?.tag
        {
            delegate?.view(self, click: CallbackAction.Tap, argument: [SWKey.index:index])
        }
    }
    
    @objc private func moreBtnTapped()
    {
        delegate?.view(self, click: CallbackAction.More, argument: nil)
    }
    
}

fileprivate class SWPoetryViewMainCell:UIView
{
    var poetry:SWPoetry?{
        didSet{
            updateView()
        }
    }
    
    private let ContentTextLeading:CGFloat = 4
    private let ContentFont = UIFont.systemFont(ofSize: 16)
    private var _titleLabel:UILabel = UILabel()
    private var _artistLabel:UILabel = UILabel()
    private var _contentLabel:UILabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        backgroundColor = UIColor.white
        
        _titleLabel.font = UIFont.systemFont(ofSize: 18)
        _titleLabel.textColor = UIColor(hex: 0x2f3231)
        _titleLabel.textAlignment = .center
        _titleLabel.frame = CGRectMake(0, 15, width, 22)
        addSubview(_titleLabel)
        
//        _artistLabel.font = UIFont.systemFont(ofSize: 14)
//        _artistLabel.textAlignment = .center
//        _artistLabel.textColor = UIColor(hex: 0xd8d8d8)
//        _artistLabel.frame = CGRectMake(0, _titleLabel.bottom + 15, width, 18)
//        addSubview(_artistLabel)
        
        _contentLabel.textColor = UIColor(hex: 0x323232)
        _contentLabel.numberOfLines = 0
        _contentLabel.frame = CGRectMake(0, _titleLabel.bottom + 15, width, 0)
        addSubview(_contentLabel)
    }
    
    private func updateView()
    {
        if let poetry = poetry
        {
            //let artist:String = poetry.artist ?? ""
            //let dynasty:String = poetry.dynasty ?? ""
            _titleLabel.text = poetry.name
            //_artistLabel.text = "【作者】 \(artist) 【时期】 \(dynasty)"
            
            let content:String = poetry.summary ?? ""
            let textHeight = StringUtil.getStringHeight(content, font: ContentFont, width: width, lineSpacing: ContentTextLeading)
            let attributedString:NSMutableAttributedString = NSMutableAttributedString(string:content)
            let textRange = NSRange(location: 0, length: content.length)
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineBreakMode = .byTruncatingMiddle
            paragraphStyle.lineSpacing = ContentTextLeading
            paragraphStyle.alignment = .center
            attributedString.addAttribute(NSFontAttributeName, value: ContentFont, range: textRange)
            attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: textRange)
            
            _contentLabel.attributedText = attributedString
            _contentLabel.height = textHeight
            
            let bottomMargin:CGFloat = 3
            height = _contentLabel.bottom + bottomMargin
        }
    }
    
}



fileprivate class SWPoetryViewCell:UIView
{
    var poetry:SWPoetry?{
        didSet{
            updateView()
        }
    }
    
    private var _titleLabel:UILabel = UILabel()
    private var _categoryLabel:UILabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        backgroundColor = UIColor.white
        
        let categoryLabelWidth:CGFloat = 120
        let categoryLabelHeight:CGFloat = 18
        let categoryLabelX:CGFloat = width - RightMargin - categoryLabelWidth
        _categoryLabel.font = UIFont.systemFont(ofSize: 14)
        _categoryLabel.textAlignment = .right
        _categoryLabel.textColor = UIColor(hex: 0xe3e3e3)
        _categoryLabel.frame = CGRectMake(categoryLabelX, (height - categoryLabelHeight) * 0.5, categoryLabelWidth, categoryLabelHeight)
        addSubview(_categoryLabel)
        
        let titleLabelX:CGFloat = LeftMargin
        let titleLabelHeight:CGFloat = 21
        _titleLabel.font = UIFont.systemFont(ofSize: 17)
        _titleLabel.textColor = UIColor(hex: 0x2f3231)
        _titleLabel.frame = CGRectMake(titleLabelX, (height - titleLabelHeight) * 0.5, categoryLabelX - titleLabelX, titleLabelHeight)
        addSubview(_titleLabel)
    }
    
    private func updateView()
    {
        if let poetry = poetry
        {
            _titleLabel.text = poetry.name
            _categoryLabel.text = poetry.summary
        }
    }
}

